#!/usr/bin/python
# vi: et sw=2
#
# Control class

import pygtk
import gtk
import gobject

from AppletGConfig import *

from AppletView import *
from PulseGlib  import *

# Controller
class PulseApplet(gtk.Image):
  def __init__(self, applet, iid):
    gtk.Image.__init__(self)

    self.applet = applet
    self.iid    = iid

    self.settings = AppletGConfig()
    self.settings.connect(
        ["layout", "theme", "use_xml_icons"],
        self.update_view_settings
    )   

    self.model = AppletModel()
    self.view  = AppletView(self.settings.get_view_settings(), self.model)

    self.pulse = PulseGlib()
    self.pulse.connect("context-ready", self.on_pulse_context_ready)
    self.pulse.connect("context-failed", self.on_pulse_context_failed)
    self.pulse.connect("context-terminated", self.on_pulse_context_terminated)

    box = gtk.HBox(False, 2)

    # FIXME this could be readed from config
    self.icon_size = 24 #gtk.ICON_SIZE_MENU

    # FIXME check is applet in top or bottom or left or right panel
    #self.applet.set_property("width-request", self.icon_size)
    self.applet.set_property("height-request", self.icon_size)

    self.view.icon_size = self.icon_size

    #self.set_from_stock(gtk.STOCK_DISCONNECT, self.icon_size)

    #box.add(self)
    box.add(self.view.volume_icon)
    box.add(self.view.volume_label)
    self.applet.add(box)
    #self.applet.add(self.view.volume_icon)

    self.applet.connect("button-press-event", self.button_press_event)
    self.applet.connect("key-press-event", self.key_press_event)

    self.pulse.pulseConnect()

    self.applet.show_all()
    self.show()

    print "PulseApplet is now loaded with", iid
    return

    #
    # __init__ end
    #

  #
  # Functions

  def show_view(self):
      #print "Focus grab"
      #gtk.gdk.keyboard_grab(self.applet.window, True)
      #gtk.gdk.pointer_grab(
      #          self.applet.window,
      #          True,
      #          gtk.gdk.BUTTON_PRESS_MASK
      #          #| gtk.gdk.BUTTON_RELEASE_MASK
      #)
      self.view.show_all()
    #
    # show_view end
    #

  def hide_view(self):
      #print "Focus release"
      #gtk.gdk.keyboard_ungrab()
      #gtk.gdk.pointer_ungrab()

      self.view.hide()
    #
    # hide_view end
    #

  #
  # PulseAudio events

  def on_pulse_context_ready(self, pulse):
    self.set_from_stock(gtk.STOCK_CONNECT, self.icon_size)
    self.model.set_pulse(pulse)

    self.applet.connect("scroll-event", self.reroute_scroll_event)
    pass
 
    # 
    # on_pulse_context_ready end
    #

  def on_pulse_context_failed(self, pulse):
    self.set_from_stock(gtk.STOCK_DIALOG_WARNING, self.icon_size)
    pass

    #
    # on_pulse_context_failed end
    #

  def on_pulse_context_terminated(self, pulse):
    self.set_from_stock(gtk.STOCK_CANCEL, self.icon_size)
    pass
 
    # 
    # on_pulse_context_terminated end
    #

  #
  # Applet events

  def reroute_scroll_event(self, widget, event):
    # FIXME get index from configs
    index = 0

    if index >= self.model.sink_count():
      index = 0

    scroll = self.view.box.get_children()[index].scroll

    self.view.box.show_all()

    # FIXME Add left and right scroll
    if event.direction == gtk.gdk.SCROLL_DOWN:
      # This is too some kind bug in GTK ???
      scroll.emit("move-slider", gtk.SCROLL_PAGE_DOWN)
    elif event.direction == gtk.gdk.SCROLL_UP:
      scroll.emit("move-slider", gtk.SCROLL_PAGE_UP)

    # scroll.emit("scroll-event", event)
    # Does work only if view is showed once. Strange bug in GTK HScale???

    pass

    #
    # reroute_scroll_event end
    #

  def button_press_event(self, widget, event):

    if event.button == 3:
      (appletX, appletY) = self.applet.window.get_origin()
      (appletW, appletH) = self.applet.window.get_size()

      self.hide_view()

      # Event is in applet icon
      if event.x >= 0 and event.x <=  appletW \
         and event.y >= 0 and event.y <= appletH:
        return False

      return True

    if event.button == 2:
      if self.model.sink_count == 0:
        return True

      # FIXME get index from GConf
      index = 0

      if index >= self.model.sink_count():
        index = 0

      mute = self.view.box.get_children()[index].mute

      mute.emit("button-press-event", event);

      return True

    # Show or hide view
    (appletX, appletY) = self.applet.window.get_origin()
    (appletW, appletH) = self.applet.window.get_size()

    # FIXME what if it is in right size or corner?
    self.view.move(appletX, appletY + appletH)

    if self.view.get_property("visible") == True:
      self.hide_view()
    else:
      self.show_view()

    return True

    pass

    #
    # button_press_event end
    #

  def key_press_event(self, widget, event):
    if not event.type == gtk.gdk.KEY_PRESS:
      return False

    if event.keyval == 65307: # ESC
      self.hide_view()

    return True

    #
    # key_press_event end
    #

  #
  # Settings events

  def update_view_settings(self, client, *args, **kwags):
    self.view.update_settings(self.settings.get_view_settings())

    #
    # update_view_settings end
    #

  pass

  #
  # class PulseApplet end
  #
